/**
 * Created by Weizehua on 2017/1/24.
 */
import {Component, OnInit} from "@angular/core";
import {NavController} from "ionic-angular";
import {Hash} from "../../Utils/Hash";
import {NewPostRequest} from "./NewPostRequest";

@Component({
    selector: 'evaluation',
    templateUrl: "NewPostPage.html",
    providers: [NewPostRequest]
})
export class NewPostPage implements OnInit {
    imageSource = "assets/image/nin-live.png";
    input: HTMLInputElement;

    constructor(public nav: NavController,
    public newPostRequest: NewPostRequest) {
    }

    ngOnInit() {
        this.input = document.querySelector('input');
        this.input.onchange = () => this.onImageFileChanged(this.input.files[0]);
    }

    onBackClicked() {
        this.nav.pop()
    }

    onImageClicked() {
        this.input.click()
    }

    async onImageFileChanged(newFile:File){
        this.renderImage(newFile);
        await this.newPostRequest.upload(newFile);
    }

    renderImage(file) {
        let reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = (event: any) => {
            this.imageSource = reader.result;
        };
    }
}
